package com.htc.android.footprints.commonui;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.utils.StorageUtil;
import com.htc.android.footprints.utils.voice.Recorder;
import com.htc.android.footprints.vo.Footprint;

public class AudioRecordDialog extends Dialog implements View.OnClickListener,
		DialogInterface.OnDismissListener {
	private static final String TAG = "AudioRecordDialog";
	private Button mCancelButton;
	private TextView mCurrentCount;
	private int mCurrentStatus = 0;
	private TextView mDurationCount;
	private Footprint mFootprint = null;
	private ProgressBar mProgress;
	private Handler mProgressHandler = new Handler();
	private Button mRecordButton;
	private recordStateListener mRecordListener = null;
	private Recorder mRecorder;
	private long mRemainingTime = -1L;
	private Button mSaveButton;
	private StorageUtil mStorageUtil = null;
	Runnable mUpdateTimer = new Runnable() {
		public void run() {
			AudioRecordDialog.this.updateTimerView();
		}
	};
	Recorder.OnStateChangedListener mediaListener = new Recorder.OnStateChangedListener() {
		public void onError(int paramInt) {
			Resources localResources = AudioRecordDialog.this.getContext()
					.getResources();
			Object localObject = null;
			switch (paramInt) {
			default:
			case 1:
			case 2:
			}
			while (true) {
				if (localObject != null)
					new HtcAlertDialog.Builder(
							AudioRecordDialog.this.getContext())
							.setTitle(2131361795).setIcon(17301543)
							.setMessage((CharSequence) localObject)
							.setPositiveButton(2131361893, null)
							.setCancelable(false).show();
				return;
				localObject = localResources.getString(2131361891);
				continue;
				localObject = localResources.getString(2131361892);
			}
		}

		public void onStateChanged(int paramInt) {
			AudioRecordDialog.access$502(AudioRecordDialog.this, paramInt);
			AudioRecordDialog.this.updateUI();
		}
	};

	public AudioRecordDialog(Context paramContext, int paramInt,
			Footprint paramFootprint) {
		super(paramContext, paramInt);
		this.mFootprint = paramFootprint;
		init();
	}

	public AudioRecordDialog(Context paramContext, Footprint paramFootprint) {
		super(paramContext);
		this.mFootprint = paramFootprint;
		init();
	}

	private void calRecordingTime() {
		long l1 = StorageUtil.availableStorage();
		long l2;
		if (l1 > 0L) {
			l2 = l1 / 135L;
			if (l2 > 60L)
				this.mRemainingTime = 60L;
		}
		while (true) {
			return;
			this.mRemainingTime = l2;
			continue;
			this.mRemainingTime = 0L;
		}
	}

	private void init() {
		getWindow().setBackgroundDrawable(new ColorDrawable());
		getWindow().requestFeature(1);
		initView();
		this.mStorageUtil = new StorageUtil(getContext()
				.getApplicationContext());
		this.mStorageUtil
				.SetOnChangeStorageListerner(new StorageUtil.OnChangeStorageListener() {
					public void onStorageChanged(int paramInt) {
						if (paramInt > 1) {
							AudioRecordDialog.this.terminateRecording();
							AudioRecordDialog.this.dismiss();
						}
						while (true) {
							return;
							AudioRecordDialog.this.mProgressHandler
									.post(new Runnable() {
										public void run() {
											AudioRecordDialog.this
													.calRecordingTime();
											AudioRecordDialog.this.mDurationCount
													.setText(AudioRecordDialog.this
															.setupTimer(1000L * AudioRecordDialog.this.mRemainingTime));
										}
									});
						}
					}
				});
		try {
			if (this.mFootprint.mAudioPath == null)
				;
			for (this.mRecorder = new Recorder();; this.mRecorder = new Recorder(
					this.mFootprint.mAudioPath)) {
				this.mRecorder.setOnStateChangedListener(this.mediaListener);
				this.mProgressHandler.post(new Runnable() {
					public void run() {
						AudioRecordDialog.this.calRecordingTime();
						AudioRecordDialog.this.mDurationCount.setText(AudioRecordDialog.this
								.setupTimer(1000L * AudioRecordDialog.this.mRemainingTime));
					}
				});
				return;
			}
		} catch (NullPointerException localNullPointerException) {
			while (true)
				localNullPointerException.printStackTrace();
		}
	}

	private void initView() {
		setOnDismissListener(this);
		View localView = LayoutInflater.from(getContext()).inflate(
				voice_record_dialog, null);
		this.mProgress = ((ProgressBar) localView.findViewById(2131492968));
		this.mDurationCount = ((TextView) localView.findViewById(2131493028));
		this.mDurationCount.setText(setupTimer(0L));
		this.mCurrentCount = ((TextView) localView.findViewById(2131493027));
		this.mCurrentCount.setText(setupTimer(0L));
		this.mSaveButton = ((Button) localView.findViewById(2131492896));
		this.mSaveButton.setOnClickListener(this);
		this.mCancelButton = ((Button) localView.findViewById(2131492897));
		this.mCancelButton.setOnClickListener(this);
		this.mRecordButton = ((Button) localView.findViewById(2131493029));
		this.mRecordButton.setOnClickListener(this);
		setContentView(localView);
		setCanceledOnTouchOutside(true);
	}

	private String setupTimer(long paramLong) {
		if (paramLong < 0L)
			;
		Object[] arrayOfObject;
		for (String str = "--:--";; str = String.format("%02d:%02d",
				arrayOfObject)) {
			return str;
			arrayOfObject = new Object[2];
			arrayOfObject[0] = Long.valueOf(paramLong / 1000L / 60L);
			arrayOfObject[1] = Long.valueOf(paramLong / 1000L % 60L);
		}
	}

	private void updateControlState(int paramInt) {
		switch (paramInt) {
		case 2:
		default:
		case 0:
		case 1:
		}
		while (true) {
			return;
			this.mRecordButton.setText(2131361921);
			this.mRecordButton.setBackgroundResource(2130837509);
			continue;
			this.mRecordButton.setText(2131361920);
			this.mRecordButton.setBackgroundResource(2130837510);
		}
	}

	private void updateTimerView() {
		int i;
		if (this.mCurrentStatus == 1) {
			i = 1;
			if ((this.mCurrentStatus == 1) && (this.mRecorder != null)) {
				this.mCurrentCount
						.setText(setupTimer(this.mRecorder.progress()));
				int j = this.mProgress.getMax();
				if (this.mRemainingTime <= 0L)
					break label162;
				this.mProgress.setProgress((int) (j * this.mRecorder.progress()
						/ this.mRemainingTime / 1000L));
				this.mDurationCount
						.setText(setupTimer(1000L * this.mRemainingTime));
				if (this.mRecorder.progress() > 1000L * this.mRemainingTime) {
					terminateRecording();
					dismiss();
				}
			}
		}
		while (true) {
			if (i != 0)
				this.mProgressHandler.postDelayed(this.mUpdateTimer, 1000L);
			return;
			i = 0;
			break;
			label162: this.mProgress.setProgress(0);
			this.mDurationCount.setText(setupTimer(-1L));
		}
	}

	private void updateUI() {
		updateControlState(this.mCurrentStatus);
		updateTimerView();
	}

	public void onClick(View paramView) {
		if (!paramView.isEnabled())
			;
		while (true) {
			return;
			if (paramView == this.mSaveButton) {
				if (this.mCurrentStatus == 1) {
					FootprintUtils.showToast(getContext(), getContext()
							.getResources().getString(2131361922));
					continue;
				}
				if (this.mRecorder.sampleFile() != null) {
					this.mFootprint.mAudioPath = this.mRecorder.sampleFile()
							.getAbsolutePath();
					this.mFootprint.mAudioFileName = this.mRecorder
							.sampleFile().getName();
					this.mRecordListener.OnRecorded(1, this.mFootprint);
				}
				dismiss();
				continue;
			}
			if (paramView == this.mCancelButton) {
				if (this.mCurrentStatus == 1) {
					FootprintUtils.showToast(getContext(), getContext()
							.getResources().getString(2131361922));
					continue;
				}
				this.mRecordListener.OnRecorded(0, this.mFootprint);
				dismiss();
				continue;
			}
			if (paramView != this.mRecordButton)
				continue;
			if ((StorageUtil.isExternalStorageEnough())
					&& (StorageUtil.isInternalStorageEnough()))
				break;
			FootprintUtils.showToast(getContext(), getContext().getResources()
					.getString(2131361887));
		}
		if (this.mCurrentStatus == 0) {
			this.mRecorder.startRecording();
			getWindow().addFlags(128);
		}
		while (true) {
			updateUI();
			break;
			this.mRecorder.stopRecording();
			getWindow().clearFlags(128);
		}
	}

	public void onDismiss(DialogInterface paramDialogInterface) {
		getWindow().clearFlags(128);
		if (this.mStorageUtil != null)
			this.mStorageUtil.closeStorgaeUtil();
		if (this.mRecorder == null)
			;
		while (true) {
			return;
			this.mRecorder.stop();
			if ((this.mFootprint == null)
					|| (this.mRecorder.sampleFile() == null))
				continue;
			this.mFootprint.mAudioPath = this.mRecorder.sampleFile()
					.getAbsolutePath();
			this.mFootprint.mAudioFileName = this.mRecorder.sampleFile()
					.getName();
		}
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if ((paramInt == 4) && (paramKeyEvent.getRepeatCount() == 0)) {
			terminateRecording();
			dismiss();
		}
		while (true) {
			return super.onKeyDown(paramInt, paramKeyEvent);
			if (((paramInt != 19) && (paramInt != 20) && (paramInt != 21) && (paramInt != 22))
					|| (this.mRecordButton.isFocusable()))
				continue;
			this.mRecordButton.setFocusable(true);
			this.mSaveButton.setFocusable(true);
			this.mCancelButton.setFocusable(true);
		}
	}

	protected void onStop() {
		this.mRecorder.stop();
		this.mRecorder = null;
		super.onStop();
	}

	public void setRecordStatusListener(
			recordStateListener paramrecordStateListener) {
		if (paramrecordStateListener != null)
			this.mRecordListener = paramrecordStateListener;
	}

	public void terminateRecording() {
		getWindow().clearFlags(128);
		if (this.mRecorder == null)
			;
		while (true) {
			return;
			this.mRecorder.stop();
			if (this.mRecorder.sampleFile() == null)
				continue;
			this.mFootprint.mAudioPath = this.mRecorder.sampleFile()
					.getAbsolutePath();
			this.mFootprint.mAudioFileName = this.mRecorder.sampleFile()
					.getName();
			this.mRecordListener.OnRecorded(1, this.mFootprint);
		}
	}

	public static abstract interface recordStateListener {
		public static final int NEW_RECORD = 1;
		public static final int NO_RECORD;

		public abstract void OnRecorded(int paramInt, Footprint paramFootprint);
	}
}

/*
 * Location:
 * E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name: com.htc.android.footprints.commonui.AudioRecordDialog JD-Core
 * Version: 0.6.0
 */